package com.example.demo.blockqueue;

import java.util.concurrent.TimeUnit;

/**
 * @desc: <br/>
 * @author: Jingbo Hu<br/>
 * @time：2021年01⽉22⽇<br/>
 * @version：v1.0.0<br>
 */
public class VolatileDemo2 {

    public static volatile boolean flag = true;

    /**
     * 线程之间的通信
     * @param args
     */
    public static void main(String[] args) {

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (flag) {
                    try {
                        System.out.println("线程正在执行......");
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();

        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("停止执行......");
                flag = false;
            }
        }).start();
    }
}
